package com.ssly.weixin.xichang.admin.controller.view;

import com.ssly.weixin.xichang.admin.domain.Admin;
import com.ssly.weixin.xichang.admin.repository.AdminRepository;
import com.ssly.weixin.xichang.common.domain.Report;
import com.ssly.weixin.xichang.common.repository.ReportRepository;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;


@Controller("admin")
@RequestMapping("/admin")
public class IndexController {
    @Autowired
    private AdminRepository adminRepository;
    @Autowired
    private ReportRepository reportRepository;
    @Value("${ssly.upload.dir}")
    private String uploadDir;

    @GetMapping
    public String index() {
        return "admin/index";
    }
    @GetMapping("weixin-article")
    public String weixinArticle() {
        return "admin/weixin-article";
    }
    @GetMapping("/login")
    public String login() {
//        Admin admin = new Admin();
//        admin.setUsername("admin");
//        String password = new BCryptPasswordEncoder().encode("xichang123456");
//
//
//        admin.setPassword(password);
//        adminRepository.save(admin);

        return "admin/login";
    }
    @GetMapping("/report/{id}")
    public String report(@PathVariable Integer id,Model model) {
        Report report = reportRepository.findById(id).get();
        report.setView(1);
        reportRepository.save(report);
        model.addAttribute("report",report);
        model.addAttribute("uploadDir",uploadDir);
        List videos = new JSONArray(report.getVideos()).toList();
        List pictures = new JSONArray(report.getPictures()).toList();

        model.addAttribute("videos",videos);
        model.addAttribute("pictures",pictures);
        return "admin/view";
    }

}
